﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace politicsWorks.UserControls
{
    public partial class UC_Base : UserControl
    {
        private const int UNDO = 0;
        private const int RIGHT = 1;
        private const int WRONG = 2;
        private Button[] buttons;
        private Button[] SelectButtons;
        private FormMain.Problem[] problems;
        private int index = 0;
        private int total = 20;
        public UC_Base(ref FormMain.Problem[] problems)
        {
            this.problems = problems;
            InitializeComponent();
            total = problems.Length;
            GenerateButtons(total);
            delegateButtons();
            GenerateSelections();
            delegateSelectButtons();
            InitUI();
            //InitProblems();
            reload();
        }

        private void InitUI()
        {
            if (total > 0)
                buttons[0].PerformClick();
            else
            {
                pictureBoxRight.Visible = false;
                pictureBoxWrong.Visible = false;
            }
        }

        private void InitProblems() //用于读取problems
        {
            throw new NotImplementedException();
        }

        public void GenerateButtons(int num)    //生成题号按钮
        {
            buttons = new Button[num];
            for (int i = 0; i < num; i++)
            {
                buttons[i] = new Button();
                buttons[i].Tag = i;
                buttons[i].Text = (i + 1).ToString();
                buttons[i].FlatStyle = FlatStyle.Flat;
                buttons[i].Size = new Size(47, 37);
                flowLayoutPanel1.Controls.Add(buttons[i]);
            }
        }

        public void delegateButtons()   //注册题号按钮
        {
            foreach (Button button in buttons)
            {
                button.Click += Button_Click;
            }
        }

        private void Button_Click(object sender, EventArgs e)   //题号按钮Click
        {
            Button thisButton = (Button)sender;
            index = (int)thisButton.Tag;
            thisButton.Focus();
            if (problems[index].result == UNDO)     //没做过
            {
                buttonRedo.PerformClick();
                return;
            }
            foreach (Button button in SelectButtons)        //已经做过的题目直接判断对错
                button.BackColor = Color.FromArgb(0, 71, 160);
            int select = SelectionConvert(problems[index].select);
            SelectButtonLoad(SelectButtons[select]);
        }

        public void GenerateSelections()       //生成选择按钮
        {
            SelectButtons = new Button[4];
            SelectButtons[0] = buttonA;
            SelectButtons[1] = buttonB;
            SelectButtons[2] = buttonC;
            SelectButtons[3] = buttonD;
        }

        public void delegateSelectButtons()     //注册选择按钮
        {
            foreach (Button button in SelectButtons)
            {
                button.Click += SelectButton_Click;
            }
        }

        private void SelectButton_Click(object sender, EventArgs e)     //选择按钮Click
        {
            if (total > 0)
            {
                if (problems[index].result == UNDO)
                {
                    SelectButtonLoad((Button)sender);
                }
            }
        }

        public void SelectButtonLoad(Button button)     //重新载入题目并判断对错
        {
            richTextBoxQ.Text = "\t" + problems[index].question;
            problems[index].select = button.Text;
            if (string.Equals(button.Text, problems[index].answer))
                RightAnswer(button);
            else
                WrongAnswer(button);
            richTextBoxA.Text = "\t" + problems[index].resolution;
        }

        public void InitRightWrong()       //重置该题目的正误
        {
            problems[index].result = UNDO;
            buttons[index].BackColor = Color.White;
            pictureBoxRight.Visible = false;
            pictureBoxWrong.Visible = false;
            foreach (Button button in SelectButtons)
                button.BackColor = Color.FromArgb(0, 71, 160);
        }

        private void WrongAnswer(Button SelectButton)
        {
            problems[index].result = WRONG;
            buttons[index].BackColor = Color.Crimson;
            pictureBoxRight.Visible = false;
            pictureBoxWrong.Visible = true;
            SelectButton.BackColor = Color.Crimson;
        }

        public void RightAnswer(Button SelectButton)
        {
            problems[index].result = RIGHT;
            buttons[index].BackColor = Color.SeaGreen;
            pictureBoxWrong.Visible = false;
            pictureBoxRight.Visible = true;
            SelectButton.BackColor = Color.SeaGreen;
        }


        private void buttonRedo_Click(object sender, EventArgs e)   //redo按钮Click
        {
            if (total > 0)
            {
                InitRightWrong();
                richTextBoxA.Text = "";
                richTextBoxQ.Text = "\t" + problems[index].question;
            }
        }

        private void buttonBefore_Click(object sender, EventArgs e) //上一题
        {
            if (index - 1 >= 0)
            {
                index--;
                buttons[index].PerformClick();
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)   //下一题
        {
            if (index + 1 < total)
            {
                index++;
                buttons[index].PerformClick();
            }
        }

        public int SelectionConvert(string selection)   //大写字母String转int
        {
            char[] arr = selection.ToCharArray();
            return (arr[0] - 65);
        }

        public void reload()
        {
            for (int i = 0; i < problems.Length; i++)
            {
                if (problems[i].result != UNDO)
                {
                    if (string.Equals(problems[i].answer, problems[i].select))
                        buttons[i].BackColor = Color.SeaGreen;
                    else
                        buttons[i].BackColor = Color.Crimson;
                }    
            }
        }
    }
}
